package com.wll.flutter.hybrid;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;

import io.flutter.app.FlutterFragmentActivity;
import io.flutter.embedding.android.FlutterFragment;
import io.flutter.embedding.android.RenderMode;
import io.flutter.embedding.android.TransparencyMode;
import io.flutter.view.FlutterView;

/**
 * created by detachment on 2020/7/31
 */
public class MyActivity extends FlutterFragmentActivity implements IShowMessage {
    public final static String INIT_PARAMS = "initParams";
    private UIPresenter uiPresenter;
    private BasicMessageChannelPlugin basicMessageChannelPlugin;
    private EventChannelPlugin eventChannelPlugin;

    // Define a tag String to represent the FlutterFragment within this
    // Activity's FragmentManager. This value can be whatever you'd like.
    private static final String TAG_FLUTTER_FRAGMENT = "flutter_fragment";

    // Declare a local variable to reference the FlutterFragment so that you
    // can forward calls to it later.
    private FlutterFragment flutterFragment;

    public static void start(Context context, String initParams) {
        Intent intent = new Intent(context, MyActivity.class);
        intent.putExtra(INIT_PARAMS, initParams);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Inflate a layout that has a container for your FlutterFragment.
        // For this example, assume that a FrameLayout exists with an ID of
        // R.id.fragment_container.
        setContentView(R.layout.my_activity_layout);

        // Get a reference to the Activity's FragmentManager to add a new
        // FlutterFragment, or find an existing one.
        FragmentManager fragmentManager = getSupportFragmentManager();

        // Attempt to find an existing FlutterFragment,
        // in case this is not the first time that onCreate() was run.
        flutterFragment = (FlutterFragment) fragmentManager
                .findFragmentByTag(TAG_FLUTTER_FRAGMENT);

        // Create and attach a FlutterFragment if one does not exist.
//        if (flutterFragment == null) {

//            flutterFragment = FlutterFragment.createDefault();

            // 使用指定的初始路线运行Flutter
//            // With a new FlutterEngine.
//            flutterFragment = FlutterFragment.withNewEngine()
//                    .initialRoute("myInitialRoute/")
//                    .build();

//            // With a new FlutterEngine.
//            flutterFragment = FlutterFragment.withNewEngine()
//                    .initialRoute("your/route/here")
//                    .build();

            // 从指定的入口点运行Flutter
//        flutterFragment = FlutterFragment.withNewEngine()
//                .dartEntrypoint("mySpecialEntrypoint")
//                .build();

            // 控制FlutterFragment的渲染模式
//        // With a new FlutterEngine.
//        flutterFragment = FlutterFragment.withNewEngine()
//                .renderMode(RenderMode.texture)
//                .build();

//        // With a cached FlutterEngine.
//        flutterFragment = FlutterFragment.withCachedEngine("my_engine_id")
//                .renderMode(RenderMode.texture)
//                .build();

            // 显示FlutterFragment透明
//        // Using a new FlutterEngine.
//        flutterFragment = FlutterFragment.withNewEngine()
//                .transparencyMode(TransparencyMode.transparent)
//                .build();

//        // Using a cached FlutterEngine.
//        flutterFragment = FlutterFragment.withCachedEngine("my_engine_id")
//                .transparencyMode(TransparencyMode.transparent)
//                .build();

            // FlutterFragment和它之间的关系Activity
//        // Using a new FlutterEngine.
//        flutterFragment = FlutterFragment.withNewEngine()
//                .shouldAttachEngineToActivity(false)
//                .build();

//        // Using a cached FlutterEngine.
//        flutterFragment = FlutterFragment.withCachedEngine("my_engine_id")
//                .shouldAttachEngineToActivity(false)
//                .build();

//            fragmentManager
//                    .beginTransaction()
//                    .add(R.id.fragment_container, flutterFragment, TAG_FLUTTER_FRAGMENT)
//                    .commit();

            String initParams = getIntent().getStringExtra(INIT_PARAMS);

//            Bundle bundle = new Bundle();
//            bundle.putString(INIT_PARAMS, initParams);
//            flutterFragment.setArguments(bundle);

//            FlutterView flutterView = Flutter.createView(this, getLifecycle(), initParams);
            FlutterView flutterView = this.getFlutterView();
            flutterView.setInitialRoute(initParams);

            FrameLayout.LayoutParams layout =
                    new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

            setContentView(flutterView, layout);

            eventChannelPlugin = EventChannelPlugin.registerWith(flutterView);
            //注册Flutter plugin
            MethodChannelPlugin.registerWith(flutterView);
            basicMessageChannelPlugin = BasicMessageChannelPlugin.registerWith(flutterView);
            uiPresenter = new UIPresenter(this, "通信与混合开发", this);
//        }

    }

    @Override
    public void onShowMessage(String message) {
        uiPresenter.showDartMessage(message);
    }

    @Override
    public void sendMessage(String message, boolean useEventChannel) {
        if (useEventChannel) {
            eventChannelPlugin.send(message);
        } else {
            basicMessageChannelPlugin.send(message, this::onShowMessage);
        }
    }

    @Override
    public void onPostResume() {
        super.onPostResume();
//        flutterFragment.onPostResume();
    }

    @Override
    protected void onNewIntent(@NonNull Intent intent) {
        super.onNewIntent(intent);
//        flutterFragment.onNewIntent(intent);
    }

    @Override
    public void onBackPressed() {
//        flutterFragment.onBackPressed();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//        flutterFragment.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public void onUserLeaveHint() {
//        flutterFragment.onUserLeaveHint();
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
//        flutterFragment.onTrimMemory(level);
    }
}

